﻿function IClock(obj,tag){
  var W = window;
  var TAG = tag;
  var D = document;
  var G = {
    'layer':null,
    'thread':{},
    'ch':0,
    'cm':0,
    'up':{
      'H':null,
      'M':null
    },
    'down':{
      'H':null,
      'M':null
    },
    'page':{
      'H':null,
      'M':null
    },
    'images':{
      'base':'images/base.png',
      'bg':'images/bg.png',
      'u0':'images/0u.png',
      'u1':'images/1u.png',
      'u2':'images/2u.png',
      'u3':'images/3u.png',
      'u4':'images/4u.png',
      'u5':'images/5u.png',
      'u6':'images/6u.png',
      'u7':'images/7u.png',
      'u8':'images/8u.png',
      'u9':'images/9u.png',
      'd0':'images/0d.png',
      'd1':'images/1d.png',
      'd2':'images/2d.png',
      'd3':'images/3d.png',
      'd4':'images/4d.png',
      'd5':'images/5d.png',
      'd6':'images/6d.png',
      'd7':'images/7d.png',
      'd8':'images/8d.png',
      'd9':'images/9d.png',
      'a0':'images/animate/0.png',
      'a1':'images/animate/1.png',
      'a2':'images/animate/2.png',
      'a3':'images/animate/3.png',
      'a4':'images/animate/4.png',
      'a5':'images/animate/5.png',
      'a6':'images/animate/6.png',
      'a7':'images/animate/7.png',
      'a8':'images/animate/8.png',
      'a9':'images/animate/9.png',
      'a10':'images/animate/10.png',
      'a11':'images/animate/11.png',
      'a12':'images/animate/12.png',
      'a13':'images/animate/13.png',
      'a14':'images/animate/14.png',
      'a15':'images/animate/15.png',
      'a16':'images/animate/16.png',
      'a17':'images/animate/17.png',
      'a18':'images/animate/18.png',
      'a19':'images/animate/19.png',
      'a20':'images/animate/20.png',
      'a21':'images/animate/21.png'
    }
  };
  var MT = {
    'T':[],
    'clear':function(){
      this.T = [];
      return this;
    },
    'add':function(u){
      this.T.push({'state':0,'dom':null,'url':u});
      return this;
    },
    'load':function(){
      var t = this.T;
      for(var i=0;i<t.length;i++){
        var o = t[i];
        o.state = 1;
        o.dom = new Image();
        o.dom.src = o.url;
        o.dom.setAttribute('index',i+'');
        if(o.dom.complete){
          o.state = 2;
        }else{
          o.dom.onload = function(){
            t[parseInt(this.getAttribute('index'),10)].state = 2;
          };
          o.dom.onerror = function(){
            t[parseInt(this.getAttribute('index'),10)].state = 3;
          };
        }
      }
    },
    'isLoaded':function(i){
      return 2==this.T[i].state;
    },
    'isCompleted':function(i){
      for(var i=0;i<this.T.length;i++){
        if(2!=this.T[i].state) return false;
      }
      return true;
    },
    'get':function(i){
      return this.T[i].dom;
    }
  };
  
  var _thread = function(){
    var a = arguments;
    var o = {
      /** 定时器 */
      'timer':null,
      /** 睡眠毫秒数 */
      'sleep':a.length>2?a[2]:12,
      /** 计数器 */
      'count':0,
      /** 状态 0未启动 1运行中 2暂停 */
      'state':0,
      /** 方法体 */
      'run':a[1],
      /**
       * 启动
       */
      'start':function(){
        var k = this;
        if(0 == k.state){
          k.state = 1;
          k.timer = W.setInterval(function(){
            switch(k.state){
              case 1:
                k.run(k.count);
                k.count ++;
                break;
            }
          },k.sleep);
        }
      },
      /**
       * 停止
       */
      'stop':function(){
        if(1 == this.state || 2 == this.state){
          W.clearInterval(this.timer);
          this.timer = null;
          delete G.thread[a[0]];
        }
      },
      /**
       * 暂停
       */
      'pause':function(){
        if(1 == this.state){
          this.state = 2;
        }
      },
      /**
       * 唤醒
       */
      'resume':function(){
        if(2 == this.state){
          this.state = 1;
        }
      }
    };
    G.thread[a[0]] = o;
    return o;
  };

  var css = function(o,s){
    try{o.style.cssText = s;}catch(e){
      try{o.setAttribute('style',s);}catch(ex){}
    }
  };
  
  var animate = function(w,n){
    if(G.thread['page'+w+'Thread']){
      G.thread['page'+w+'Thread'].stop();
    }
    _thread('page'+w+'Thread',function(l){
      if(l>22){
        G.thread['page'+w+'Thread'].stop();
        G.down[w].style.marginLeft = (0-n*100)+'px';
      }else{
        G.page[w].style.marginLeft = (0-l*129)+'px';
        if(l==12){
          G.up[w].style.marginLeft = (0-n*100)+'px';
        }
      }
    },20).start();
  };
  
  var update = function(h,m){
    if(G.ch!=h){
      G.ch = h;
      animate('H',h);
    }
    if(G.cm!=m){
      G.cm = m;
      animate('M',m);
    }
  };
  
  var initUp = function(t){
    var o = D.createElement('div');
    css(o,'width:100px;height:40px;margin:0;padding:0;font-size:0;overflow:hidden');
    o.style.marginLeft = ('H'==t?36:170)+'px';
    o.style.marginTop = ('H'==t?-118:-40)+'px';
    G.layer.appendChild(o);
    var m = D.createElement('div');
    css(m,'width:'+(100*('H'==t?24:60))+'px;height:40px;margin:0;padding:0;font-size:0;overflow:hidden');
    o.appendChild(m);
    G.up[t] = m;
    for(var i=0;i<('H'==t?24:60);i++){
      var n = D.createElement('img');
      n.ondragstart = function(){return false;};
      css(n,'width:48px;height:40px;margin:0;padding:0');
      n.style.marginTop = '-40px';
      n.src = G.images['u'+Math.floor(i/10)];
      m.appendChild(n);
      
      n = D.createElement('img');
      n.ondragstart = function(){return false;};
      css(n,'width:48px;height:40px;margin:0;padding:0');
      n.style.marginLeft = '4px';
      n.style.marginTop = (0==i?0:-40)+'px';
      n.src = G.images['u'+Math.floor(i%10)];
      m.appendChild(n);
    }
	return o;
  };
  
  var initDown = function(t){
    var o = D.createElement('div');
    css(o,'width:100px;height:43px;margin:0;padding:0;font-size:0;overflow:hidden');
    o.style.marginLeft = ('H'==t?36:170)+'px';
    o.style.marginTop = ('H'==t?0:-43)+'px';
    G.layer.appendChild(o);
    var m = D.createElement('div');
    css(m,'width:'+(100*('H'==t?24:60))+'px;height:43px;margin:0;padding:0;font-size:0;overflow:hidden');
    o.appendChild(m);
    G.down[t] = m;
    for(var i=0;i<('H'==t?24:60);i++){
      var n = D.createElement('img');
      n.ondragstart = function(){return false;};
      css(n,'width:48px;height:43px;margin:0;padding:0');
      n.style.marginTop = '-43px';
      n.src = G.images['d'+Math.floor(i/10)];
      m.appendChild(n);
      
      n = D.createElement('img');
      n.ondragstart = function(){return false;};
      css(n,'width:48px;height:43px;margin:0;padding:0');
      n.style.marginLeft = '4px';
      n.style.marginTop = (0==i?0:-40)+'px';
      n.src = G.images['d'+Math.floor(i%10)];
      m.appendChild(n);
    }
	return o;
  };
  
  var initPage = function(t){
    var o = D.createElement('div');
    css(o,'width:129px;height:142px;margin:0;padding:0;font-size:0;overflow:hidden');
    o.style.marginLeft = ('H'==t?20:154)+'px';
    o.style.marginTop = '-142px';
    G.layer.appendChild(o);
    var m = D.createElement('div');
    css(m,'width:'+(23*129)+'px;height:142px;margin:0;padding:0;font-size:0;overflow:hidden');
    o.appendChild(m);
    G.page[t] = m;
    for(var i=0;i<21;i++){
      var n = D.createElement('img');
      n.ondragstart = function(){return false;};
      css(n,'width:129px;height:142px;margin:0;padding:0');
      n.style.marginLeft = (0==i?129:0)+'px';
      n.style.marginTop = (0==i?0:-142)+'px';
      n.src = G.images['a'+i];
      m.appendChild(n);
    }
	return o;
  };
  
  var initBg = function(t){
    var o = D.createElement('div');
    css(o,'width:129px;height:142px;margin:0;padding:0;font-size:0;overflow:hidden;background-image:url(\''+G.images['bg']+'\');background-position:0 0;background-repeat:no-repeat');
    o.style.marginLeft = ('H'==t?20:154)+'px';
    o.style.marginTop = ('H'==t?0:-142)+'px';
    G.layer.appendChild(o);
	return o;
  };
  
  var init = function(){
    G.layer.style.backgroundImage = 'url(\''+G.images['base']+'\')';
    G.layer.style.backgroundPosition = '0 29px';
    G.layer.style.backgroundRepeat = 'repeat-x';

			initBg('H');
		    initBg('M');
			initPage('H');
			initPage('M');
			initUp('H');
			initUp('M');
			initDown('H');
			initDown('M');
    
    _thread('timeThread',function(l){
      var now = new Date();
			update(now.getHours(),now.getMinutes());
    },1000).start();
  };
  
  var bind = function(p){
    var o = D.createElement('div');
    css(o,'width:303px;height:200px;margin:0;padding:0;font-size:0');
    p.appendChild(o);	
    G.layer = o;
    MT.clear();
    for(var i in G.images){
      MT.add(G.images[i]);
    }
    MT.load();
    if(G.thread['loadThread']){
      G.thread['loadThread'].stop();
    }
    _thread('loadThread',function(l){
      if(MT.isCompleted()){
        G.thread['loadThread'].stop();
        init();
      }
    },40).start();
  };
  
  bind(obj);
}